package com.example.my_gobang.game;

import com.example.my_gobang.config.WebSocketConfig;
import org.springframework.stereotype.Component;
import org.springframework.web.socket.WebSocketSession;

import java.util.HashMap;
import java.util.concurrent.ConcurrentHashMap;

/**
 * Creat with IntelliJ IDEA
 * Description:
 * User:WZW
 * Date:2022-08-20
 * Time:13:49
 */
@Component
public class OnlineUserManager {
    // 用于存储游戏大厅中在线的用户会话
    private ConcurrentHashMap<Integer, WebSocketSession> gameHall = new ConcurrentHashMap<>();
    // 用户在游戏房间中的在线状态
    private ConcurrentHashMap<Integer, WebSocketSession> gameRoom = new ConcurrentHashMap<>();

    public void enterGameHall (int userId, WebSocketSession webSocketSession) {
        gameHall.put(userId,webSocketSession);
    }

    public void enterGameRoom (int userId, WebSocketSession webSocketSession) {
        gameRoom.put(userId,webSocketSession);
    }

    public void exitGameHall (int userId) {
        gameHall.remove(userId);
    }

    public void exitGameRoom (int userId) {
        gameRoom.remove(userId);
    }

    public WebSocketSession getFromGameHall(int userId) {
        return gameHall.get(userId);
    }

    public WebSocketSession getFromGameRoom(int userId) {
        return gameRoom.get(userId);
    }
}
